package com.taptech.doufu.ui.view.choice.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.choice.CategoryTitleModel;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceCategoryTitleAdapter extends RecyclerView.Adapter<ChoiceCategoryTitleAdapter.ViewHolder>{

    private List<CategoryTitleModel> list;
    private Context context;
    private int selectPos;
    private OnClickItem onClickItem;
    public ChoiceCategoryTitleAdapter(Context context, List<CategoryTitleModel> list, OnClickItem onClickItem){
        this.list = list;
        this.context = context;
        this.onClickItem = onClickItem;
        onClickItem.onClick(list.get(0));
    }

    public interface OnClickItem{
        void onClick(CategoryTitleModel model);
    }

    public CategoryTitleModel getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }

    public void refrush(List<CategoryTitleModel> list) {
        if (list != null) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.novel_category_title_item, null);
        final ViewHolder holder = new ViewHolder(view);
        holder.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        holder.lay = view.findViewById(R.id.lay);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CategoryTitleModel model = getItem(position);
        if (model != null) {
            holder.tvTitle.setText(model.getTitle());
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickItem.onClick(model);
                    selectPos = position;
                    notifyDataSetChanged();
                }
            });
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                if (selectPos == position) {
                    holder.lay.setBackgroundResource(R.drawable.cycle_red_night);
                    holder.tvTitle.setTextColor(Color.parseColor("#ffffff"));
                }else{
                    holder.lay.setBackgroundResource(R.drawable.bg_tag_gray_dark);
                    holder.tvTitle.setTextColor(ContextCompat.getColor(context,R.color.text_color_6C788A));
                }
            }else {
                if (selectPos == position) {
                    holder.lay.setBackgroundResource(R.drawable.cycle_red);
                    holder.tvTitle.setTextColor(Color.parseColor("#ffffff"));
                }else{
                    holder.lay.setBackgroundResource(R.drawable.bg_tag_gray);
                    holder.tvTitle.setTextColor(ContextCompat.getColor(context,R.color.text_color_6C788A));
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_22));
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
    }
}
